/*
 RLRecyclingLog.m
 Date Created: February 18, 2010
 
 Author: Karol Krizka <kkrizka@gmail.com>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "RLRecyclingLog.h"

#import "PMProfile.h"

@implementation RLRecyclingLog 

@dynamic runs;
@dynamic owner;

// Used in runsOrderedByDate
NSInteger compareRunsByDate(id date1,id date2,void *context) {
  return [[date1 dateTime] compare:[date2 dateTime]];
}

- (RLRecyclingRun *)createRun {
  RLRecyclingRun *newRun=[NSEntityDescription insertNewObjectForEntityForName:@"RecyclingRun"
                                                       inManagedObjectContext:[self managedObjectContext]];
  newRun.dateTime=[NSDate date];
  [self addRunsObject:newRun];
  
	return newRun;
}

- (NSArray *)runsOrderedByDate:(BOOL)ascending {
  NSArray *allRuns=[self.runs allObjects];
  NSArray *ascendingRuns=[allRuns sortedArrayUsingFunction:compareRunsByDate context:nil];
  if(ascending)
    return ascendingRuns;

  // For descending runs, we have to reverse the ascending array
  NSMutableArray *descendingRuns=[ascendingRuns mutableCopy];
  
  for(int i=0;i<descendingRuns.count/2;i++) {
    [descendingRuns exchangeObjectAtIndex:i withObjectAtIndex:descendingRuns.count-i-1];
  }
  
  return [NSArray arrayWithArray:descendingRuns];
}

- (unsigned int)lastMonthTotal {
  NSCalendar *calendar=[NSCalendar currentCalendar];
  NSDate *now=[NSDate date];
  
  NSUInteger unitFlags=NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
  NSDateComponents *dateComponents=[calendar components:unitFlags fromDate:now];
  dateComponents.day=1;
  NSDate *endDate=[calendar dateFromComponents:dateComponents];

  dateComponents.month=dateComponents.month-1;
  if(dateComponents.month<=0) { // Last month is December, of last year..
    dateComponents.month=12;
    dateComponents.year=dateComponents.year-1;
  }
  NSDate *startDate=[calendar dateFromComponents:dateComponents];
  
	return [self totalForPeriodStartingAt:startDate endingAt:endDate];
}

- (NSUInteger)thisMonthTotal {
  NSCalendar *calendar=[NSCalendar currentCalendar];
  NSDate *now=[NSDate date];
  
  NSUInteger unitFlags=NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
  NSDateComponents *dateComponents=[calendar components:unitFlags fromDate:now];
  dateComponents.day=1;
  NSDate *startDate=[calendar dateFromComponents:dateComponents];
	return [self totalForPeriodStartingAt:startDate endingAt:now];
}

- (NSUInteger)overallTotal {
  NSUInteger total=0;
  for(RLRecyclingRun *run in self.runs) {
    total+=run.moneyEarned;
  }
  return total;
}

- (NSUInteger)totalForPeriodStartingAt:(NSDate *)startDate
                              endingAt:(NSDate*)endDate {
  NSUInteger total=0;
  for(RLRecyclingRun *run in self.runs) {
    if([run.dateTime compare:startDate]!=NSOrderedAscending &&
       [run.dateTime compare:endDate]!=NSOrderedDescending) {
      total+=run.moneyEarned;
    }
  }
  return total;
}

@end
